<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/8/9
 * Time: 15:48
 */
class RabbitAMQ
{
    private $connection = null;

    function __construct()
    {
        $this->connection = new AMQPConnection(array(
            'host' => '10.116.204.42',
            'port' => '5672',
            'vhost' => 'pink_server',
            'login' => 'pink_server',
            'password' => '6Up9noEOpdG2'));
        $this->connection->connect() or $this->connection = null;
    }

    function __destruct()
    {
        if ($this->connection != null) {
            $this->connection->disconnect();
        }
    }

    function publish($message, $exchangeName, $queueName, $routeKey)
    {
        if (empty($message) || empty($exchangeName)) {
            return;
        }
        try {
            $channel = new AMQPChannel($this->connection);
            $exchange = new AMQPExchange($channel);
            $exchange->setName($exchangeName);
            $queue = new AMQPQueue($channel);
            $queue->setName($queueName);
            $queue->bind($exchangeName, $queue->getName());
            $exchange->publish($message, $routeKey);
        } catch (AMQPConnectionException $e) {
            UK::saveLog('AMQPConnectio.error.log', print_r($e, true) . "\r\n", TRUE);
        }
    }
}

class Gearman
{
    public $client = null;
    private $host = '10.47.109.31';
    private $port = 4730;

    function __construct()
    {
        self::$client = new GearmanClient();
        if (!empty(self::$client)) {
            self::$client->addServer($this->host, $this->port);
        }
    }

}

class Queue
{
    private $rabbitAMQ = null;
    private $gearman = null;

    function __construct()
    {
        $gearman = new Gearman();
        $this->gearman = $gearman->client;
    }

    public function rebuildUserChatGroupRelations($do, $data)
    {
        if (empty($do) || empty($data)) return false;

        if (!empty($this->gearman)) {
            $this->gearman->doBackground($do, $data);
        }
    }
}